package com.ruiysoft.service.statistics.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruiysoft.entity.statistics.ShopUserStatistics;
import com.ruiysoft.mapper.statistics.ShopUserStatisticsMapper;
import com.ruiysoft.service.statistics.IShopUserStatisticsService;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author liyahui
 * @since 2019-03-25
 */
@Service
public class ShopUserStatisticsServiceImpl extends ServiceImpl<ShopUserStatisticsMapper, ShopUserStatistics> implements IShopUserStatisticsService {

    @Override
    public IPage<Map<String,Object>> selectPageStatistics(Integer pageStart, Integer pageSize, Long shopId, Long userId, Long skuId, Integer type, Long sTime, Long eTime) {
        IPage<Map<String,Object>> mapPage= pageMaps(new Page(pageStart,pageSize),new QueryWrapper<ShopUserStatistics>()
                .select("COUNT(DISTINCT user_id) num,DATE_FORMAT(in_time,'%Y%m%d') days")
                .eq(type!=null,"type",type)
                .eq(shopId!=null,"shop_id",shopId)
                .eq(userId!=null,"user_id",userId)
                .eq(skuId!=null,"sku_id",skuId)
                .between("in_time",sTime!=null?new Date(sTime):new Date(0L),eTime!=null?new Date(eTime):new Date())
                .orderBy(false,false,"").groupBy("days"));

        return mapPage;
    }

    @Override
    public void insertAsyc(ShopUserStatistics shopUserStatistics) {
        save(shopUserStatistics);
    }
}
